<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('休假类型')" prop="type">
            <el-input v-model="form.type" :placeholder="$t('休假类型')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('默认天数')" prop="day">
            <el-input-number v-model="form.day" :controls="false" :precision="1" :max="1000" :placeholder="$t('默认天数')" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否跨年')" prop="whetherYear">
            <el-radio-group v-model="form.whetherYear" @change="whetherYearChange">
              <el-radio
                v-for="(dict, index) in sysYesOrNoOptions"
                :key="index"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.whetherYear === 'Y'" :span="12">
          <el-form-item :label="$t('跨年日期')" prop="yearDate" class="hello">
            <DatePicker
              v-model="form.yearDate"
              type="date"
              format="MM-dd"
              :placeholder="$t('跨年日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="sort">
            <!--            <el-input-number v-model="form.sort" :controls="false" :precision="1" :placeholder="$t('默认天数')" show-word-limit />-->
            <el-input-number v-model="form.sort" controls-position="right" :min="1" :max="1000" show-word-limit />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发放形式')" prop="whetherUserDivisionAge">
            <el-radio-group v-model="form.whetherUserDivisionAge" @change="updateWhetherUserDivisionAge">
              <el-radio
                v-for="(dict, index) in whetherUserDivisionAgeOptions"
                :key="index"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.whetherUserDivisionAge === 'SL' || form.whetherUserDivisionAge === 'GL'" :span="12">
          <el-form-item :label="$t('发放日期方式')" prop="releaseDateType">
            <el-select v-model="form.releaseDateType" value="" :placeholder="$t('发放日期方式')">
              <el-option
                v-for="dict in releaseDateTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div v-if="form.whetherUserDivisionAge === 'SL' || form.whetherUserDivisionAge === 'GL'">
        <div class="area-header">
          <span class="area-header-title">{{ form.whetherUserDivisionAge === 'SL' ? $t('司龄配额列表') : $t('工龄配额列表') }}</span>
        </div>
        <el-table :data="form.oaVacationTypeDetailList" border class="nowrapTable formTable">
          <el-table-column :label="$t('序号')" align="center" width="50">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column :label="form.whetherUserDivisionAge === 'SL' ? $t('司龄>=年份') : $t('工龄>=年份')" align="center">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ form.whetherUserDivisionAge === 'SL' ? $t('司龄>=年份') : $t('工龄>=年份') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaVacationTypeDetailList.' + scope.$index + '.divisionAgeYear'" :rules="oaVacationTypeDetailCheck.divisionAgeYear">
                <el-input-number v-model="scope.row.divisionAgeYear" controls-position="right" :min="0" :max="9999" show-word-limit />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('享有假期天数')" align="center">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('享有假期天数') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'oaVacationTypeDetailList.' + scope.$index + '.day'" :rules="oaVacationTypeDetailCheck.day">
                <el-input-number v-model="scope.row.day" controls-position="right" :min="0" :max="1000" show-word-limit />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('操作')" align="center" width="60px">
            <template slot-scope="scope">
              <el-button size="small" type="text" icon="el-icon-delete" @click="delRow(scope.$index, scope.row)" />
            </template>
          </el-table-column>
        </el-table>
        <div class="tol">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="addRow">{{ $t('添加') }}</el-button>
        </div>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;margin: 10px 0 0;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="oa:vacationtype" />
    </div>
  </div>
</template>

<script>
    import { saveOaVacationType, getOaVacationType } from '@/api/oa/vacation/type/oaVacationType'
    import { delOaVacationTypeDetail } from '@/api/oa/vacation/vacationTypeDetail/oaVacationTypeDetail'
    export default {
        data() {
          var checkSort = (rule, value, callback) => {
            if (value <= 0) {
              callback(new Error(this.$t('排序不能小于或等于0')))
            } else {
              callback()
            }
          }
            return {
                // 表单参数
                form: {
                  oaVacationTypeDetailList: []
                },
                // 遮罩层
                loading: false,
                // 系统是否字典
                sysYesOrNoOptions: [],
                // 发放时间方式类型字典
                releaseDateTypeOptions: [],
                // 假期发放形式字典
                whetherUserDivisionAgeOptions: [],
                // 表单校验
                rules: {
                  type: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], day: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], yearDate: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], releaseDateType: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], whetherUserDivisionAge: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ], sort: [
                    { validator: checkSort, trigger: ['blur', 'change'] }
                  ]
                },
                oaVacationTypeDetailCheck: {
                  divisionAgeYear: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }],
                  day: [{ required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }]
                }
            }
        },
        created() {
          this.getDicts('sys_yes_no').then(response => {
            this.sysYesOrNoOptions = response.data
          })
          this.getDicts('holidy_grant_type').then(response => {
            this.whetherUserDivisionAgeOptions = response.data
          })
          this.getDicts('release_date_type').then(response => {
            this.releaseDateTypeOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getOaVacationType(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
          // 添加出差事由
          addRow() {
            const row = {
              id: undefined,
              vacationTypeId: undefined,
              divisionAgeYear: undefined,
              day: undefined,
              createBy: undefined,
              createTime: undefined,
              updateBy: undefined,
              updateTime: undefined,
              delFlag: undefined
            }
            this.form.oaVacationTypeDetailList.push(row)
          },
          // 删除出差事由
          delRow(index, row) {
            const id = row.id
            if (id !== undefined && id !== '' && id != null) {
              this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
              }).then(function() {
                return delOaVacationTypeDetail(id)
              }).then(() => {
                this.form.oaVacationTypeDetailList.splice(index, 1)
                return this.msgSuccess(this.$t('删除成功'))
              }).catch(function() {
              })
            } else {
              this.form.oaVacationTypeDetailList.splice(index, 1)
            }
          },
          // 切换的时候删除list数据
          updateWhetherUserDivisionAge() {
            this.form.oaVacationTypeDetailList = []
          },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                type: undefined,
                day: undefined,
                whetherYear: 'N',
                whetherUserDivisionAge: 'ZDY',
                yearDate: undefined,
                releaseDateType: undefined,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                oaVacationTypeDetailList: [],
                sort: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveOaVacationType(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          whetherYearChange(val) {
            val === 'Y' ? this.form.yearDate = undefined : this.form.yearDate = undefined
          }

        }

    }
</script>

<style lang="scss" scoped>
  .el-date-picker__header >>> .el-date-picker__header-label:nth-of-type(1) {
    display: none;
  }
  .el-date-picker__header >>> .el-icon-d-arrow-left, .el-date-picker__header >>> .el-icon-d-arrow-right {
    display: none;
  }
</style>
